package com.hunan.api.catv.service.biz.member.login;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.LoginType;
import com.hunan.api.catv.entity.enums.SmsTypeEnum;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.member.LoginService;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.sms.SmsBiz;
import com.hunan.api.catv.service.biz.wx.WxBiz;
import com.hunan.api.catv.service.impl.member.MemberService;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.MessageFormat;

@Service
public class WechatLoginService extends ResultService implements LoginService {

    @Autowired
    MemberService memberService;

    @Autowired
    WxBiz wxBiz;

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    SmsBiz smsBiz;

    @Override
    public ServiceResult login(String id, String phone, String sms, String ip, String pushToken, String... extParams) {
        if (!ObjectUtils.isEmpty(id)) {
            String wxUserInfoKey = MessageFormat.format(CacheKey.member_wxUserInfo, id);
            JsonObject memberJson = new JsonObject();
            MemberVo memberVo = new MemberVo();
            //授权
            if (ObjectUtils.isEmpty(redisUtils.get(wxUserInfoKey))) {
                ServiceResult tokenResult = wxBiz.getAccessToken(id);
                if (!tokenResult.getStatus()) {
                    return tokenResult;
                }
                JsonObject tokenJson = GsonUtils.fromJson(tokenResult.getData().toString(), JsonObject.class);
                String openid = tokenJson.get("openid").getAsString();
                //微信个人资料
                memberVo = memberBiz.infoById(LoginType.wechat.getCode(), openid);
                String accessToken = tokenJson.get("access_token").getAsString();
                ServiceResult userResult = wxBiz.getUserInfo(accessToken, openid);
                if (!userResult.getStatus()) {
                    return userResult;
                }
                redisUtils.set(wxUserInfoKey, userResult.getData().toString(), 7200);
                if (ObjectUtils.isEmpty(memberVo)) {
                    memberJson.addProperty("isBind", true);
//					memberJson.add("member",null);
                    return buildOK(memberJson, "");
                }
                //更新资料
                JsonObject userInfo = GsonUtils.fromJson(userResult.getData().toString(), JsonObject.class);
                String headimgurl = userInfo.get("headimgurl").getAsString();
                String nickName = userInfo.get("nickname").getAsString();
                if (!memberVo.getAvatarUrl().equals(headimgurl) || !memberVo.getNickName().equals(nickName)) {
                    ServiceResult result = memberBiz.updateId(LoginType.wechat.getCode(), memberVo.getMemberId(), id, headimgurl, nickName);
                    if (!result.status) {
                        return buildFail(ConstantsMsg.sync_wxUserInfo_fail);
                    }
                }
                memberVo = memberBiz.infoById(LoginType.wechat.getCode(), openid);
                memberJson.addProperty("isBind", false);
                memberJson.addProperty("member", GsonUtils.toJson(memberVo));
                return buildOK(memberJson, "");
            }
            //绑定手机号
            if (!ObjectUtils.isEmpty(phone) && !ObjectUtils.isEmpty(sms)) {
                ServiceResult checkSmsResult = smsBiz.checkSmsCode(SmsTypeEnum.BOUND.getCode(), phone, sms);
                if (!checkSmsResult.getStatus()) {
                    return checkSmsResult;
                }
                //验证该手机号是否被绑定
                ServiceResult checkPhoneResult = memberBiz.checkPhoneId(LoginType.wechat.getCode(), phone, id);
                if (!checkPhoneResult.getStatus()) {
                    return checkPhoneResult;
                }
                //绑定/注册
                ServiceResult boundResult = memberBiz.boundPhone(LoginType.wechat.getCode(), id, phone);
                if (!boundResult.getStatus()) {
                    return boundResult;
                }
                memberJson.addProperty("isBind", false);
                memberVo = memberBiz.getByPhone(phone);
                memberJson.addProperty("member", GsonUtils.toJson(memberVo));
                redisUtils.del(MessageFormat.format(CacheKey.member_wxUserInfo, id));
                return buildOK(memberJson, "");
            }
        }
        return buildFail(false, ConstantsMsg.wx_auth_fail);
    }

    @Override
    public ServiceResult bind(String id, Integer loginType, Integer memberId) {
        // TODO Auto-generated method stub
        return null;
    }

}
